var api = getApp();

Page({
    data: {
        tabActive: ["active", ""],
        search: {
            keywords: "",
            page: 2,
            clear: "",
            result: []
        },
        hotMusic: [],
        originalMusicInfo: {},
        musicInfo: {},
        searchReturn: !1,
        rcmd_list: 0,
        hot_music_title: true,
        showMusic: -1,
        show_music: -1,
        page: 1,
        so_key: '',
        search_list:[]
    },
    tapChoose: function() {
        this.setData({
            tabActive: ["active", ""]
        });
    },
    tapSearch: function() {
        this.setData({
            tabActive: ["", "active"]
        });
    },
    tapCategory: function(t) {
        var e = t.currentTarget.dataset.index,
            a = this.data.musicList;
        "active" === a[e].active ? a.forEach(function(t, e) {
            t.active = "", -1 !== t.list.findIndex(function(t) {
                return "selected" === t.selected;
            }) && (t.selected = "selected");
        }) : (a.forEach(function(t, e) {
            t.active = "", -1 !== t.list.findIndex(function(t) {
                return "selected" === t.selected;
            }) && (t.selected = "selected");
        }), a[e].active = "active", a[e].selected = "");
        this.setData({
            musicList: a
        });
    },
    selectSong: function(t) {
        var a = this, c = t.currentTarget.dataset;
        a.data.musicInfo = c;
        a.data.show_music = c.music_id, a.data.showMusic = -1;
        a.setData({ 
            show_music: a.data.show_music, 
            showMusic: a.data.showMusic
         });
        wx.playBackgroundAudio({
            dataUrl: c.music_url,
            success: function () {
                console.log("音乐地址：", c.music_url);
            }
        });
    },
    selectSearchResult: function(t) {
        var a = this, c = t.currentTarget.dataset;
        a.data.musicInfo = c;
        a.data.showMusic = c.index, a.data.show_music = -1;
        a.setData({ 
            showMusic: a.data.showMusic,
            show_music: a.data.show_music
        });
        wx.playBackgroundAudio({
            dataUrl: c.music_url,
            success: function() {
                console.log("搜索的音乐地址：", c.music_url);
            }
        }), a.data.show_music = '';
    },
    onSearchInput: function(t) {
        if (t.detail.value !== ""){
            this.search(t.detail.value);
        }
    },
    search: function(key) {
        this.data.page = 1;
        this.data.so_key = key;
        this.search_so_up(key, this.data.page);
        this.setData({ page: this.data.page })
    },
    search_so: function(t){
        var e = t.detail.value;
        this.data.page = 1;
        this.data.so_key = e;
        this.search_so_up(e, this.data.page);
        this.setData({ page: this.data.page})
    },
    search_so_up: function (key, page){
        var a = this, page = page;
        wx.showLoading({ title: "加载中" });
        wx.request({
            url: api.globalData.Api + '/musicSearch',
            method: 'POST',
            data: {
                keyword: key,
                page: page
            },
            header: api.header,
            success: function (res) {
                if (res.data.status == 1) {
                    if (res.data.data.num !== 0) {
                        if(page == 1){
                            a.data.search_list = res.data.data.list
                            a.setData({
                                searchList: res.data.data.list,
                                hot_music_title: false
                            }), wx.hideLoading();
                        }else{
                            if (res.data.data.list.length == 0) {
                                wx.showToast({
                                    title: '已全部加载',
                                    icon: 'none'
                                })
                            } else {
                                a.setData({
                                    searchList: a.data.search_list.concat(res.data.data.list),
                                    hot_music_title: false,
                                    page: page
                                }), wx.hideLoading();
                                a.data.search_list = a.data.search_list.concat(res.data.data.list)
                            }
                        }
                    } else { 
                        console.log('音乐搜索异常，请稍后再试！')
                    }

                }
            }
        })
    },
    finishChooseMusic: function() {
        api.globalData.editArticleMusic = this.data.musicInfo, wx.stopBackgroundAudio(), wx.navigateBack();
    },
    onLoad: function(e) {
        var a = this;
        wx.showLoading({ title: "加载中" });
        wx.request({
            url: api.globalData.Api + '/musicList',
            data: '',
            method: 'GET',
            dataType: 'json',
            success: function(res) {
                a.setData({
                    musicList: res.data,
                }), wx.hideLoading();
            }
        });
        wx.request({
            url: api.globalData.Api + '/musicSearchList',
            data: '',
            method: 'GET',
            dataType: 'json',
            success: function (res) {
                a.setData({
                    hotMusic: res.data.data,
                    hot_music_title: a.data.hot_music_title
                }), wx.hideLoading();
                
            }
        });
    },
    getHotMusic: function() {
        var t = this;
        e.hot().then(function(e) {
            console.log(e), 0 == e.data.code && t.setData({
                hotMusic: e.data.data
            });
        });
    },
    searchHot: function(t) {
        var e = t.currentTarget.dataset.hot;
        this.setData({
            "search.keywords": e
        }), this.search(e);
    },
    onReady: function() {
        wx.setNavigationBarTitle({
            title: "添加音乐"
        });
    },
    onUnload: function() {
        wx.stopBackgroundAudio();
    },
    onReachBottom: function() {
        var page = this.data.page + 1, k = this.data.so_key;
        if ("active" === this.data.tabActive[1]){
            this.search_so_up(k, page);
        }
    }
});